<?php
header("content-type:text/html;charset=utf-8");
/*订单*/
define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');
/*订单列表*/
if($_REQUEST['act']=="list"){
	//查询订单
	$sql="select * from ".ly()."order_info ";
	$where="where add_time !=''";
	$order="ORDER BY order_id DESC";
	//订单编号
	if($order_sn=$_REQUEST['order_sn']){
		$where.="and order_sn like '%{$order_sn}%'";
	}
	//收货人
	if($consignee=$_REQUEST['consignee']){
		$where.="and consignee like '%{$consignee}%'";
	}
	//订单状态
	$zt=$_REQUEST['zt'];
	if($zt=='1'){
		$where.="and order_status='0'";
	}elseif($zt=='2'){
		$where.="and pay_status='0' and order_status in('0','1','5')";
	}elseif($zt=='3'){
		$where.="and shipping_status in ('0','3','5') and order_status ='1' and pay_status='2'";
	}elseif($zt=='4'){
		$where.="and order_status='5' and pay_status='2' and shipping_status in('1','2')";
	}elseif($zt=='5'){
		$where.="and pay_status='1'";
	}elseif($zt=='6'){
		$where.="and order_status='2'";
	}elseif($zt=='7'){
		$where.="and order_status='3'";
	}elseif($zt=='8'){
		$where.="and order_status='4'";
	}elseif($zt=='9'){
		$where.="and shipping_status='4'";
	}
	$num='10';
	$sql.=$where.$order;
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
	$res=$db->getAll($sql);
	foreach ($res as $key =>&$v) {
		$v['goods_amount']=$v['goods_amount']+$v['discount']+$v['tax']+$v['shipping_fee']+$v['insure_fee']+$v['pay_fee']+$v['pack_fee']+$v['card_fee'];
	}
	$smarty->assign('order_sn',$order_sn);
	$smarty->assign('consignee',$consignee);
	$smarty->assign('zt',$zt);
	$smarty->assign('order_list',$res);
	$smarty->display('order_list.html');
}elseif($_REQUEST['act']=='edit'){
	//订单详情
	$order_id=$_REQUEST['order_id'];
	$sql="select * from ".ly()."order_info where order_id='{$order_id}'";
	$res=$db->getRow($sql);
	$user_id=$res['user_id'];
	if($user_id>'0'){
		$sql="select * from ".ly()."users where user_id='{$user_id}'";
		$user=$db->getRow($sql);
		if (!empty($user))
        {
            $res['user_name'] = $user['user_name'];
        }
	}
	//收货地址
	$sql2 = "SELECT concat(IFNULL(c.region_name, ''), '  ', IFNULL(p.region_name, ''), " .
                "'  ', IFNULL(t.region_name, ''), '  ', IFNULL(d.region_name, '')) AS region " .
            "FROM " .ly(). "order_info AS o " .
                "LEFT JOIN " .ly(). "region AS c ON o.country = c.region_id " .
                "LEFT JOIN " .ly(). "region AS p ON o.province = p.region_id " .
                "LEFT JOIN " .ly(). "region AS t ON o.city = t.region_id " .
                "LEFT JOIN " .ly(). "region AS d ON o.district = d.region_id " .
            "WHERE o.order_id = '" . $res['order_id'] . "'";
    $res['region'] = $db->getOne($sql2);
    //订单商品
    $sqla="select a.goods_name,a.goods_sn,a.goods_price,a.goods_number,b.goods_number number from ".ly()."order_goods a,".ly()."goods b where a.order_id='{$order_id}' and a.goods_id=b.goods_id";
    $resa=$db->getAll($sqla);
    //操作记录
    $sqlb="select * from ".ly()."order_action where order_id='{$order_id}' and action_place='0' ORDER BY action_id DESC ";
    $resb=$db->getAll($sqlb);
    //订单总金额
    $res['total_fee']=$res['goods_amount']+$res['discount']+$res['tax']+$res['shipping_fee']+$res['insure_fee']+$res['pay_fee']+$res['pack_fee']+$res['card_fee'];
    //应付款金额
    $res['order_amount']=$res['total_fee']-$res['money_paid']-$res['surplus']-$res['integral_money']-$res['bonus'];
   	$smarty->assign('order_log',$resb);
   	$smarty->assign('goods_list',$resa);
	$smarty->assign('edit',$res);
	$smarty->display('order_edit.html');
}elseif($_REQUEST['act']=='upd'){
	/*修改订单状态*/
	$order_id=$_REQUEST['order_id'];
	$goods_amount=$_REQUEST['order_amount'];
	$confirm=$_REQUEST['confirm'];//确认
	if($confirm){
		$sql="update ".ly()."order_info set order_status='1' where order_id='{$order_id}'";
		$db->query($sql);
		$time=time();
		$sql="insert into ".ly()."order_action(order_id,action_user,order_status,
		shipping_status,pay_status,log_time)values('{$order_id}','{$_SESSION['admin_name']}','1','0','0',$time)";
		$res=$db->query($sql);
		if($res){
			$href[]=array('text'=>"返回订单详情",'href'=>"order.php?act=edit&order_id=$order_id");
         	sys_msg("操作成功！",$href);
     	}else{
     		$href[]=array('text'=>"返回订单详情",'href'=>"order_id.php?act=edit&order_id=$order_id");
         	sys_msg("操作失败！",$href);
		}
	}
	$payment=$_REQUEST['payment'];//付款
	if($payment){
		$sql="update ".ly()."order_info set order_status='1',pay_status='2',money_paid='{$goods_amount}',order_amount='0' where order_id='{$order_id}'";
		$db->query($sql);
		$time=time();
		$sql="insert into ".ly()."order_action(order_id,action_user,order_status,
		shipping_status,pay_status,log_time)values('{$order_id}','{$_SESSION['admin_name']}','1','0','2',$time)";
		$res=$db->query($sql);
		if($res){
			$href[]=array('text'=>"返回订单详情",'href'=>"order.php?act=edit&order_id=$order_id");
         	sys_msg("操作成功！",$href);
     	}else{
     		$href[]=array('text'=>"返回订单详情",'href'=>"order_id.php?act=edit&order_id=$order_id");
         	sys_msg("操作失败！",$href);
		}
	}
	$unpaid=$_REQUEST['unpaid'];//设置未付款
	if($unpaid){
		$sql="update ".ly()."order_info set order_status='1',shipping_status='0',pay_status='0',money_paid='0',order_amount='{$goods_amount}' where order_id='{$order_id}'";
		$db->query($sql);
		$time=time();
		$sql="insert into ".ly()."order_action(order_id,action_user,order_status,
		shipping_status,pay_status,log_time)values('{$order_id}','{$_SESSION['admin_name']}','1','0','0',$time)";
		$res=$db->query($sql);
		if($res){
			$href[]=array('text'=>"返回订单详情",'href'=>"order.php?act=edit&order_id=$order_id");
         	sys_msg("操作成功！",$href);
     	}else{
     		$href[]=array('text'=>"返回订单详情",'href'=>"order_id.php?act=edit&order_id=$order_id");
         	sys_msg("操作失败！",$href);
		}
	}
	$cancel=$_REQUEST['cancel'];//取消
	if($cancel){
		$sql="update ".ly()."order_info set order_status='2',pay_status='0',shipping_status='0',money_paid='0',order_amount='{$goods_amount}' where order_id='{$order_id}'";
		$db->query($sql);
		$time=time();
		$sql="insert into ".ly()."order_action(order_id,action_user,order_status,
		shipping_status,pay_status,log_time)values('{$order_id}','{$_SESSION['admin_name']}','2','0','0',$time)";
		$res=$db->query($sql);
		if($res){
			$href[]=array('text'=>"返回订单详情",'href'=>"order.php?act=edit&order_id=$order_id");
         	sys_msg("操作成功！",$href);
     	}else{
     		$href[]=array('text'=>"返回订单详情",'href'=>"order_id.php?act=edit&order_id=$order_id");
         	sys_msg("操作失败！",$href);
		}
	}
	$invalid=$_REQUEST['invalid'];//无效
	if($invalid){
		$sql="update ".ly()."order_info set order_status='3',pay_status='0',shipping_status='0',money_paid='0',order_amount='{$goods_amount}' where order_id='{$order_id}'";
		$db->query($sql);
		$time=time();
		$sql="insert into ".ly()."order_action(order_id,action_user,order_status,
		shipping_status,pay_status,log_time)values('{$order_id}','{$_SESSION['admin_name']}','3','0','0',$time)";
		$res=$db->query($sql);
		if($res){
			$href[]=array('text'=>"返回订单详情",'href'=>"order.php?act=edit&order_id=$order_id");
         	sys_msg("操作成功！",$href);
     	}else{
     		$href[]=array('text'=>"返回订单详情",'href'=>"order_id.php?act=edit&order_id=$order_id");
         	sys_msg("操作失败！",$href);
		}
	}
	$invoice=$_REQUEST['invoice'];//生成发货单
	if($invoice){
	$admin_info=admin_info();
	$sql="select * from ".ly()."order_info where order_id='{$order_id}'";
	$res=$db->getRow($sql);
  	$res['insure_yn'] = empty($res['insure_fee']) ? 0 : 1;
	$user_id=$res['user_id'];
	if($user_id>'0'){
		$sql="select * from ".ly()."users where user_id='{$user_id}'";
		$user=$db->getRow($sql);
		if (!empty($user))
        {
            $res['user_name'] = $user['user_name'];
        }
	}
	   /* 取得区域名 */
    $sql2 = "SELECT concat(IFNULL(c.region_name, ''), '  ', IFNULL(p.region_name, ''), " .
                "'  ', IFNULL(t.region_name, ''), '  ', IFNULL(d.region_name, '')) AS region " .
            "FROM " .ly(). "order_info AS o " .
                "LEFT JOIN " .ly(). "region AS c ON o.country = c.region_id " .
                "LEFT JOIN " .ly(). "region AS p ON o.province = p.region_id " .
                "LEFT JOIN " .ly(). "region AS t ON o.city = t.region_id " .
                "LEFT JOIN " .ly(). "region AS d ON o.district = d.region_id " .
            "WHERE o.order_id = '" . $res['order_id'] . "'";
    $res['region'] = $db->getOne($sql2);
     //订单商品
    $sqla="select a.goods_name,a.goods_sn,a.goods_price,a.goods_number,a.send_number,b.goods_number number,(a.goods_number-a.send_number) send from ".ly()."order_goods a,".ly()."goods b where a.order_id='{$order_id}' and a.goods_id=b.goods_id";
    $resa=$db->getAll($sqla);
	$smarty->assign('admin',$admin_info);
	$smarty->assign('goods_list',$resa);
	$smarty->assign('back_order',$res);
	$smarty->display('order_invoice.html');
	}
	$delivery=$_REQUEST['delivery'];
	if($delivery){
		$sql="select * from ".ly()."delivery_order  where order_id='{$order_id}' ORDER BY delivery_id DESC";
		$res=$db->getAll($sql);
		$smarty->assign('delivery_list',$res);
		$smarty->display('order_delivery_list.html');
	}
	$receivedgoods=$_REQUEST['receivedgoods'];
	//	确认收货
	if($receivedgoods){
		$sql="update ".ly()."order_info set order_status='5',shipping_status='2',pay_status='2' where order_id='{$order_id}'";
		$res=$db->query($sql);
		if($res=='1'){
			$time=time();
			$sql="insert into ".ly()."order_action(order_id,action_user,order_status,
			shipping_status,pay_status,log_time)values('{$order_id}','{$_SESSION['admin_name']}','5','2','2',$time)";
			$res=$db->query($sql);
			if($res=='1'){
				$href[]=array('text'=>"返回订单详情",'href'=>"order.php?act=edit&order_id=$order_id");
         		sys_msg("操作成功！",$href);
     		}else{
     			$href[]=array('text'=>"返回订单详情",'href'=>"order_id.php?act=edit&order_id=$order_id");
         		sys_msg("操作失败！",$href);
			}
		}
	}
	$returngoods=$_REQUEST['returngoods'];
	//退货
	if($returngoods){
		$sql="update ".ly()."order_info set order_status='4',shipping_status='0',pay_status='0',money_paid='0',order_amount='{$goods_amount}' where order_id='{$order_id}'";
		$res=$db->query($sql);
		if($res=='1'){
			$time=time();
			$sql="insert into ".ly()."order_action(order_id,action_user,order_status,
			shipping_status,pay_status,log_time)values('{$order_id}','{$_SESSION['admin_name']}','4','0','0',$time)";
			$res=$db->query($sql);
			if($res=='1'){
				$sql="select max(delivery_id) from ".ly()."delivery_order where order_id='{$order_id}'";
				$a=$db->getone($sql);
				$sql="update ".ly()."delivery_order set status='1' where order_id='{$order_id}' and delivery_id='{$a}'";
				$res=$db->query($sql);
				if($res=='1'){
					$sql="select * from ".ly()."delivery_order where delivery_id='{$a}'";
					$res=$db->getRow($sql);
					if($res){
						$delivery_sn=$res['delivery_sn'];
						$order_sn=$res['order_sn'];
						$order_id=$res['order_id'];
						$invoice_no=$res['invoice_no'];
						$add_time=$res['add_time'];
						$shipping_id=$res['shipping_id'];
						$shipping_name=$res['shipping_name'];
						$user_id=$res['user_id'];
						$action_user=$res['action_user'];
						$consignee=$res['consignee'];
						$address=$res['address'];
						$country=$res['country'];
						$province=$res['province'];
						$city=$res['city'];
						$district=$res['district'];
						$sign_building=$res['sign_building'];
						$email=$res['email'];
						$zipcode=$res['zipcode'];
						$tel=$res['tel'];
						$mobile=$res['mobile'];
						$best_time=$res['best_time'];
						$postscript=$res['postscript'];
						$how_oos=$res['how_oos'];
						$insure_fee=$res['insure_fee'];
						$shipping_fee=$res['shipping_fee'];
						$update_time=$res['update_time'];
						$suppliers_id=$resp['suppliers_id'];
						$status='0';
						$agency_id=$res['agency_id'];
						$return_time=time();
						$sql="insert into ".ly()."back_order(delivery_sn,order_sn,order_id,invoice_no,add_time,shipping_id,shipping_name,user_id,action_user,consignee,address,country,province,city,district,sign_building,email,zipcode,tel,mobile,best_time,postscript,how_oos,insure_fee,shipping_fee,update_time,suppliers_id,status,agency_id,return_time)values('{$delivery_sn}','{$order_sn}','{$order_id}','{$invoice_no}','{$add_time}','{$shipping_id}','{$shipping_nam}','{$user_id}','{$action_user}','{$consignee}','{$address}','{$country}','{$province}','{$city}','{$district}','{$sign_building}','{$email}','{$zipcode}','{$tel}','{$mobile}','{$best_time}','{$postscript}','{$how_oos}','{$insure_fee}','{$shipping_fee}','{$update_time}','{$suppliers_id}','{$status}','{$agency_id}','{$return_time}')";
						$res=$db->query($sql);
						if($res=='1'){
							$back_id=mysql_insert_id();
							$sql="select a.goods_id,a.goods_name,c.brand_name,a.goods_sn, a.goods_number send_number  from ".ly()."order_goods a,".ly()."goods b,".ly()."brand c where a.goods_id=b.goods_id and b.brand_id=c.brand_id and a.order_id='{$order_id}'";
							$res=$db->getAll($sql);
							foreach ($res as $key => &$value) {
								$value['back_id']=$back_id;
								$value['is_real']=1;
							}
							$length=count($res);
							for($i=0;$i<$length;$i++){ 
								$arr=quzhi($res[$i]);
								$a=$arr['key'];
								$b=$arr['value'];
     							$sql="insert into ".ly()."back_goods($a) values($b)";
								$res=$db->query($sql);
							}
							if($res=='1'){
								$href[]=array('text'=>"返回订单详情",'href'=>"order.php?act=edit&order_id=$order_id");
         						sys_msg("操作成功！",$href);
     						}else{
     							$href[]=array('text'=>"返回订单详情",'href'=>"order.php?act=edit&order_id=$order_id");
         						sys_msg("操作失败！",$href);
							}
						}
					}
				}
			}
		}
	}
}elseif($_REQUEST['act']=='invoice_add'){
	/*生成发货单*/
	$order_id=$_REQUEST['order_id'];
	$send_number=$_REQUEST['send_number'];
	$invoice_no=$_REQUEST['invoice_no'];
	$order_sn=$_REQUEST['order_sn'];
	$add_time=$_REQUEST['add_time'];
	$shipping_id=$_REQUEST['shipping_id'];
	$shipping_name=$_REQUEST['shipping_name'];
	$user_id=$_REQUEST['user_id'];
	$user_name=$_REQUEST['user_name'];
	$consignee=$_REQUEST['consignee'];
	$address=$_REQUEST['address'];
	$country=$_REQUEST['country'];
	$province=$_REQUEST['province'];
	$city=$_REQUEST['city'];
	$district=$_REQUEST['district'];
	$email=$_REQUEST['email'];
	$tel=$_REQUEST['tel'];
	$how_oos=$_REQUEST['how_oos'];
	$insure_fee=$_REQUEST['insure_fee'];
	$shipping_fee=$_REQUEST['shipping_fee'];
	$a=phone_yzm();
	$delivery_sn=date('Ymd',time()).$a;
	$update_time=time();
	$sql="insert into ".ly()."delivery_order(delivery_sn,order_sn,order_id,add_time,shipping_id,shipping_name,user_id,action_user,consignee,address,country,province,city,district,email,tel,how_oos,insure_fee,shipping_fee,update_time,status)values('{$delivery_sn}','{$order_sn}','{$order_id}','{$add_time}','{$shipping_id}','{$shipping_name}','{$user_id}','{$user_name}','{$consignee}','{$address}','{$country}','{$province}','{$city}','{$district}','{$email}','{$tel}','{$how_oos}','{$insure_fee}','{$shipping_fee}','{$update_time}','2')";
		$res=$db->query($sql);
		if($res=='1'){
			$delivery_id=mysql_insert_id();
			$sql="select a.goods_id,a.goods_name,c.brand_name,a.goods_sn from ".ly()."order_goods a,".ly()."goods b,".ly()."brand c where a.goods_id=b.goods_id and b.brand_id=c.brand_id and a.order_id='{$order_id}'";
			$res=$db->getAll($sql);
			foreach ($res as $key => &$value) {
				$value['delivery_id']=$delivery_id;
				$value['is_real']=1;
				$value['send_number']=$send_number;
			}
			$length=count($res);
			for($i=0;$i<$length;$i++){ 
				$arr=quzhi($res[$i]);
				$a=$arr['key'];
				$b=$arr['value'];
     			$sql="insert into ".ly()."delivery_goods($a) values($b)";
				$res1=$db->query($sql);
			}
			if($res1=='1'){
				$sql="update ".ly()."order_info set order_status='5',shipping_status='5',pay_status='2' where order_id='{$order_id}'";
				$res=$db->query($sql);
				if($res){
					$time=time();
					$sql="insert into ".ly()."order_action(order_id,action_user,order_status,shipping_status,pay_status,log_time)values('{$order_id}','{$_SESSION['admin_name']}','5','5','2',$time)";
					$res=$db->query($sql);
				if($res){
					$href[]=array('text'=>"返回订单详情",'href'=>"order.php?act=edit&order_id=$order_id");
         			sys_msg("操作成功！",$href);
				}else{
     				$href[]=array('text'=>"返回订单详情",'href'=>"order_id.php?act=edit&order_id=$order_id");
         			sys_msg("操作失败！",$href);
				}	
			}
		}
	}	
}elseif($_REQUEST['act']=='delivery_list'){
	/*发货单列表*/
	$sql="select * from ".ly()."delivery_order ";
	$where="where add_time !=''";
	$order="ORDER BY delivery_id DESC";
	//发货单流水号
	if($delivery_sn=$_REQUEST['delivery_sn']){
		$where.= "and delivery_sn like '%{$delivery_sn}%'";
	}
	//订单编号
	if($order_sn=$_REQUEST['order_sn']){
		$where.="and order_sn like '%{$order_sn}%'";
	}
	//收货人
	if($consignee=$_REQUEST['consignee']){
		$where.="and consignee like '%{$consignee}%'";
	}
	//状态
	if($zt=$_REQUEST['zt']){
		$where.="and status='{$zt}'";
	}
	$num='10';
	$sql.=$where.$order;
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
	$res=$db->getAll($sql);
	$smarty->assign('delivery_sn',$delivery_sn);
	$smarty->assign('order_sn',$order_sn);
	$smarty->assign('consignee',$consignee);
	$smarty->assign('zt',$zt);
	$smarty->assign('delivery_list',$res);
	$smarty->display('order_delivery_list.html');
}elseif($_REQUEST['act']=='delivery_edit'){
	/*发货单详情*/
	$delivery_id=$_REQUEST['delivery_id'];
	$sql="select * from ".ly()."delivery_order where delivery_id='{$delivery_id}'";
	$res=$db->getRow($sql);
	$res['insure_yn'] = empty($res['insure_fee']) ? 0 : 1;
	$user_id=$res['user_id'];
	$order_id=$res['order_id'];
	if($user_id>'0'){
		$sql="select * from ".ly()."users where user_id='{$user_id}'";
		$user=$db->getRow($sql);
		if (!empty($user))
        {
            $res['user_name'] = $user['user_name'];
        }
	}
	   /* 取得区域名 */
    $sql2 = "SELECT concat(IFNULL(c.region_name, ''), '  ', IFNULL(p.region_name, ''), " .
                "'  ', IFNULL(t.region_name, ''), '  ', IFNULL(d.region_name, '')) AS region " .
            "FROM " .ly(). "order_info AS o " .
                "LEFT JOIN " .ly(). "region AS c ON o.country = c.region_id " .
                "LEFT JOIN " .ly(). "region AS p ON o.province = p.region_id " .
                "LEFT JOIN " .ly(). "region AS t ON o.city = t.region_id " .
                "LEFT JOIN " .ly(). "region AS d ON o.district = d.region_id " .
            "WHERE o.order_id = '" . $res['order_id'] . "'";
    $res['region'] = $db->getOne($sql2);
	$sql1="select * from ".ly()."delivery_goods where delivery_id='{$delivery_id}'";
	$res1=$db->getAll($sql1);
	$sqlb="select * from ".ly()."order_action where order_id='{$order_id}' and action_place='1' ORDER BY action_id DESC  ";
    $resb=$db->getAll($sqlb);
	$smarty->assign('order_log',$resb);
	$smarty->assign('goods_lsit',$res1);
	$smarty->assign('back_order',$res);
	$smarty->display('order_delivery_edit.html');
}
elseif($_REQUEST['act']=='deliver'){
	/*去发货*/
	/*$good  1；去发货;  2 : 取消发货*/
	$good=$_REQUEST['good'];
	$delivery_id=$_REQUEST['delivery_id'];
	$order_id=$_REQUEST['order_id'];
	$invoice_no=$_REQUEST['invoice_no'];
	if($good=='1'){
		$sql="update ".ly()."delivery_order set status='3',invoice_no='{$invoice_no}' where delivery_id='{$delivery_id}'";
		$res=$db->query($sql);
		if($res==1){
			$sql="update ".ly()."order_info set order_status='5',shipping_status='1',pay_status='2',invoice_no='{$invoice_no}' where order_id='{$order_id}'";
			$res=$db->query($sql);
			if($res=='1'){
				$time=time();
				$sql="insert into ".ly()."order_action(order_id,action_user,order_status,
				shipping_status,pay_status,log_time,action_place)values('{$order_id}','{$_SESSION['admin_name']}','1','1','2',$time,'1')";
				$res=$db->query($sql);
				if($res=='1'){
					$href[]=array('text'=>"返回发货单列表",'href'=>"order.php?act=delivery_list");
         			sys_msg("操作成功！",$href);
				}else{
     				$href[]=array('text'=>"返回发货详情",'href'=>"order.php?act=delivery_edit&delivery_id=$delivery_id");
         			sys_msg("操作失败！",$href);
				}
			} 	
		}
	}else{
		$sql="update ".ly()."delivery_order set status='2' where delivery_id='{$delivery_id}'";
		$res=$db->query($sql);
		if($res=='1'){
			$sql="update ".ly()."order_info set order_status='5',shipping_status='5',pay_status='2'，invoice_no='{$invoice_no}' where order_id='{$order_id}'";
			$res=$db->query($sql);
			if($res=='1'){
				$time=time();
				$sql="insert into ".ly()."order_action(order_id,action_user,order_status,
				shipping_status,pay_status,log_time,action_place)values('{$order_id}','{$_SESSION['admin_name']}','5','5','2',$time,'1')";
				$res=$db->query($sql);
				if($res=='1'){
					$href[]=array('text'=>"返回发货单详情",'href'=>"order.php?act=delivery_edit&delivery_id=$delivery_id");
         			sys_msg("操作成功！",$href);
				}
			}
		}
	}
}/*退货单*/
elseif($_REQUEST['act']=='back_list'){
	$sql="select * from ".ly()."back_order ";
	$where="where add_time !=''";
	$order="ORDER BY back_id DESC";
	//发货单流水号
	if($delivery_sn=$_REQUEST['delivery_sn']){
		$where.= "and delivery_sn like '%{$delivery_sn}%'";
	}
	//订单编号
	if($order_sn=$_REQUEST['order_sn']){
		$where.="and order_sn like '%{$order_sn}%'";
	}
	//收货人
	if($consignee=$_REQUEST['consignee']){
		$where.="and consignee like '%{$consignee}%'";
	}
	$num='10';
	$sql.=$where.$order;
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
	$res=$db->getAll($sql);
	$smarty->assign('delivery_sn',$delivery_sn);
	$smarty->assign('order_sn',$order_sn);
	$smarty->assign('consignee',$consignee);
	$smarty->assign('back_list',$res);
	$smarty->display('order_back_list.html');
}//退货单详情
elseif($_REQUEST['act']=='back_edit'){
	$back_id=$_REQUEST['back_id'];
	$admin_info=admin_info();
	$sql="select * from ".ly()."back_order where back_id='{$back_id}'";
	$res=$db->getRow($sql);
	$res['insure_yn'] = empty($res['insure_fee']) ? 0 : 1;
	$user_id=$res['user_id'];
	if($user_id>'0'){
		$sql="select * from ".ly()."users where user_id='{$user_id}'";
		$user=$db->getRow($sql);
		if (!empty($user))
        {
            $res['user_name'] = $user['user_name'];
        }
	}
	   /* 取得区域名 */
    $sql2 = "SELECT concat(IFNULL(c.region_name, ''), '  ', IFNULL(p.region_name, ''), " .
                "'  ', IFNULL(t.region_name, ''), '  ', IFNULL(d.region_name, '')) AS region " .
            "FROM " .ly(). "order_info AS o " .
                "LEFT JOIN " .ly(). "region AS c ON o.country = c.region_id " .
                "LEFT JOIN " .ly(). "region AS p ON o.province = p.region_id " .
                "LEFT JOIN " .ly(). "region AS t ON o.city = t.region_id " .
                "LEFT JOIN " .ly(). "region AS d ON o.district = d.region_id " .
            "WHERE o.order_id = '" . $res['order_id'] . "'";
    $res['region'] = $db->getOne($sql2);
	$sql1="select * from ".ly()."back_goods where back_id='{$back_id}'";
	$res1=$db->getAll($sql1);
	$smarty->assign('goods_lsit',$res1);
	$smarty->assign('back_order',$res);
	$smarty->display('order_back_edit.html');
}elseif($_REQUEST['act']=='excel'){
	$sql="select a.order_id,a.order_sn,b.goods_name,b.goods_sn,a.consignee,b.goods_number,a.add_time,a.order_amount,a.goods_amount,a.address,a.pay_name,a.order_status,a.pay_status,a.shipping_status from ".ly()."order_info a,".ly()."order_goods b where a.order_id=b.order_id";
	$res=$db->getAll($sql);
	foreach($res as $k =>&$row){
		if($row[order_status]=='0'){
			$row[order_status]='未确认';
		}
		if($row[order_status]=='1'){
			$row[order_status]='已确认';
		}
		if($row[order_status]=='2'){
			$row[order_status]='取消';
		}
		if($row[order_status]=='3'){
			$row[order_status]='无效';
		}
		if($row[order_status]=='4'){
			$row[order_status]='退货';
		}
		if($row[order_status]=='5'){
			$row[order_status]='已分单';
		}
		if($row[order_status]=='6'){
			$row[order_status]='部分已分单';
		}
		if($row[pay_status]=='0'){
			$row[pay_status]='未付款';
		}
		if($row[pay_status]=='1'){
			$row[pay_status]='付款中';
		}
		if($row[pay_status]=='2'){
			$row[pay_status]='已付款';
		}
		if($row[shipping_status]=='0'){
			$row[shipping_status]='未发货';
		}
		if($row[shipping_status]=='1'){
			$row[shipping_status]='已发货';
		}
		if($row[shipping_status]=='2'){
			$row[shipping_status]='收货确认';
		}
		if($row[shipping_status]=='3'){
			$row[shipping_status]='配货中';
		}
		if($row[shipping_status]=='4'){
			$row[shipping_status]='已发货(部分商品)';
		}
		if($row[shipping_status]=='5'){
			$row[shipping_status]='发货中';
		}
		if($row[add_time]){
			$row[add_time]=date("Y-m-d H:i:s", $row[add_time]); 
		}
	}
	$title="序号,订单编号,商品名称,商品货号,收货人,购买数量,下单时间,总金额,应付金额,收货地址,支付名称,订单状态,付款状态,发货状态";
	//导出订单
	$filename = date('Ymd').'.csv';
	export_csv($filename,$title,$res);
}
/*随机数*/
function phone_yzm(){
	$arr=range('0','99999');
	$str=join($arr);
	$str=str_shuffle($str);
	$str=substr($str,0,8);
  return $str;
}
//生成发货单查询数据
function quzhi($arr){
	$str=array();
	foreach ($arr as $key => $value){
		$k1.=','.$key;
 		$v1.=",'".$value."'";
	}
	$a=substr($k1,1);
	$b=substr($v1,1);
	$str['key']=$a;
	$str['value']=$b;
	return $str;
}
function export_csv($filename,$title,$data) {
	$title=iconv("utf-8","gb2312",$title)."\n";
	$str="";
	foreach($data as $v){
		foreach($v as $val){
			$str.="\t".iconv("utf-8","gb2312",$val).",";
		}
		$str.="\n";
	}
	$con=$title.$str;
    header("Content-type:text/csv");
    header("Content-Disposition:attachment;filename=".$filename);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    echo $con;
}
?>